#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
VFS节点模型 - 统一文件和目录的接口
"""

from typing import Optional, Dict, Any
from datetime import datetime


class VFSNode:
    """VFS节点 - 统一文件和目录的接口"""
    
    def __init__(self, 
                 name: str,
                 is_dir: bool,
                 created_at: datetime,
                 updated_at: datetime,
                 size: int = 0,
                 file_md5: Optional[str] = None,
                 meta: Optional[Dict[str, Any]] = None,
                 original_path: Optional[str] = None,
                 deleted_at: Optional[datetime] = None):
        self.name = name
        self.is_dir = is_dir
        self.created_at = created_at
        self.updated_at = updated_at
        self.size = size
        self.file_md5 = file_md5
        self.meta = meta or {}
        self.original_path = original_path
        self.deleted_at = deleted_at
    
    @classmethod
    def from_virtual_file(cls, virtual_file, storage=None) -> 'VFSNode':
        """从VirtualFile模型创建VFSNode"""
        meta = {}
        if virtual_file.media_type:
            meta['media_type'] = virtual_file.media_type
        if virtual_file.file_extension:
            meta['file_extension'] = virtual_file.file_extension
        if virtual_file.large_icon_url:
            meta['large_icon_url'] = virtual_file.large_icon_url
        if virtual_file.small_icon_url:
            meta['small_icon_url'] = virtual_file.small_icon_url
        
        # 如果是已删除的文件，设置原始路径信息
        original_path = None
        deleted_at = None
        if virtual_file.deleted:
            # 构建原始路径（简化版本，实际可能需要递归构建完整路径）
            original_path = virtual_file.original_name or virtual_file.name
            deleted_at = virtual_file.deleted_at
        
        return cls(
            name=virtual_file.name,
            is_dir=virtual_file.is_directory,
            created_at=virtual_file.created_at,
            updated_at=virtual_file.modified_at,
            size=virtual_file.file_size_bytes,
            file_md5=storage.file_id if storage else virtual_file.storage_file_id,
            meta=meta,
            original_path=original_path,
            deleted_at=deleted_at
        )
    
    def __repr__(self):
        return f"VFSNode(name='{self.name}', is_dir={self.is_dir}, size={self.size})"